//
//  DailyIndicatorViewController.m
//  yilutongxing
//
//  Created by T&J on 2017/6/26.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "DailyIndicatorViewController.h"
#import "SuifangSubitemViewCell.h"

@interface DailyIndicatorViewController () <UITextFieldDelegate>
@property(nonatomic,strong) NSArray *rowArray;
@property(nonatomic,strong) NSArray *propertyArr;
@end

@implementation DailyIndicatorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"日常指标";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackGroundImage"]
                                                  forBarMetrics:UIBarMetricsDefault];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存"
                                                                             style:UIBarButtonItemStyleDone
                                                                            target:self
                                                                            action:@selector(saveAction)];
    UIImage *img = [[UIImage imageNamed:@"返回"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:img
                                                                            style:UIBarButtonItemStyleDone
                                                                           target:self
                                                                           action:@selector(backAction)];

    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    self.tableView.allowsSelection = NO;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"SuifangSubitemViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
    
    self.rowArray = @[
                      @{@1:@"体温",@2:@"参考值:37℃",@3:@"℃"},
                      @{@1:@"血压(低压-舒张压)",@2:@"参考值:60-90kpa",@3:@"kpa"},
                      @{@1:@"血压(高压-收缩压)",@2:@"参考值:80-130kpa",@3:@"kpa"},
                      @{@1:@"身高",@2:@"",@3:@"cm"},
                      @{@1:@"体重",@2:@"",@3:@"kg"},
                      @{@1:@"BMI",@2:@"",@3:@""}
                      ];
    
        self.propertyArr = @[
                             @"temperature",
                             @"lowPressure",
                             @"highPressure",
                             @"height",
                             @"weight",
                             @"bmi"
                             ];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 事件响应
-(void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)saveAction{
    if ([self.delegate respondsToSelector:@selector(backDailyindicatorData:)]) {
        [self.delegate backDailyindicatorData:self.dailyIndicator];
    }
    
    [self backAction];
}

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [SuifangSubitemViewCell staticHeight];;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _rowArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *rowUIDic = _rowArray[indexPath.row];
    SuifangSubitemViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[SuifangSubitemViewCell alloc]init];
    }
    [cell renderUIWithText:rowUIDic];
    if (indexPath.row==_propertyArr.count-1) {
        cell.valTextfield.enabled = NO;
        CGFloat weight = [_dailyIndicator.weight floatValue];
        CGFloat height = [_dailyIndicator.height floatValue]/100;
        CGFloat bmi = weight/(height*height);
        cell.valTextfield.text = [NSString stringWithFormat:@"%.2f", bmi];
    }
    else {
        cell.valTextfield.tag = indexPath.row;
        cell.valTextfield.delegate = self;
        cell.valTextfield.text = [_dailyIndicator valueForKey:_propertyArr[indexPath.row]];
    }
    
    return cell;
}

- (RPDailyIndicatorBean *)dailyIndicator{
    if (!_dailyIndicator) {
        _dailyIndicator = [RPDailyIndicatorBean new];
    }
    return _dailyIndicator;
}

#pragma mark UITextField代理
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.text.length>0) {
        [self.dailyIndicator setValue:textField.text forKey:_propertyArr[textField.tag]];
    }
    [self.tableView reloadData];
}


@end
